package com.example.blog.api;

import com.example.blog.until.Result;
import com.example.blog.until.updown;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : LiuWenHao
 * @create 2023/3/7 13:24
 */
@ResponseBody
@Controller
public class webp {
    @Autowired
    updown updown;

    @PostMapping("/upload")
    public String upload(@RequestParam(value = "file", required = false) MultipartFile files) throws IOException {
        String originalFilename = files.getOriginalFilename();
        // 获取文件后缀名
        String extension = StringUtils.getFilenameExtension(originalFilename);
        // 自定义文件名
        String fileName = UUID.randomUUID().toString() + originalFilename;

        System.out.println("fileName:"+fileName);
        // 存放文件的目录
        String resourcePath = this.getClass().getResource("/").getPath();
        String dirPath = resourcePath + "images/";
        File dir = new File(dirPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        // 创建文件
        File newFile = new File("D:\\nginx-1.21.6\\html\\im\\"+fileName);
        System.out.println("newFile:"+newFile);


        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {

            bufferedInputStream = new BufferedInputStream(files.getInputStream());
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(newFile));
            byte[] arr = new byte[1024];
            int len = 0;
            while((len = bufferedInputStream.read(arr)) != -1){
                bufferedOutputStream.write(arr,0,len);
                bufferedOutputStream.flush();
            }
            System.out.println("上传成功");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("上传失败");
            return "cc";
        }finally {
            if (bufferedInputStream != null) {
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bufferedOutputStream != null){
                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        String ss="D:\\nginx-1.21.6\\html\\im\\"+fileName+".jpg";
        updown.webpToPng(String.valueOf(newFile),ss);
        //return null "上传失败!";
        return "http://localhost:9001/im/"+fileName+".jpg";
    }
}
